__author__ = "jimmyorr@gmail.com (Jimmy Orr), chedbrandh@gmail.com (Christofer Hedbrandh)"

import random

from dropio.client import *
from client_test_utils import *

class DropIoClientSubscriptionActionsTestCase(DropIoClientBaseTestCase):

    def setUp(self):
        DropIoClientBaseTestCase.setUp(self)
        # create self.drop
        self.drop = self.client.create_drop(drop_name = self.available_drop_name)
    
    ################
    # create_subscription()
    ################
    
    def test_create_subscription_by_subscription(self):
        for c in filter(None, [self.client, self.secure_client]):
            url = "http://foo.com"
            sub = c.create_subscription(url = url, drop = self.drop)
            c.delete_subscription(subscription = sub, drop = self.drop)
            sub = c.create_subscription(subscription = sub, drop = self.drop)
            self.failUnlessEqual(c.get_subscription(subscription = sub, drop = self.drop).username, url)
    
    def test_create_subscription_no_such_drop(self):
        for c in filter(None, [self.client, self.secure_client]):
            self.assertRaises(ResourceNotFoundError, c.create_subscription, drop_name = self.available_drop_name2, url = "http://foo.com")
    
    def test_create_subscription_unauthorized(self):
        assert self.client2 or self.secure_client2, "no second key specified"
        for c in filter(None, [self.client, self.secure_client]):
            for c2 in filter(None, [self.client2, self.secure_client2]):
                self.assertRaises(ForbiddenError, c2.create_subscription, drop = self.drop, url = "http://foo.com")
    
    ################
    # get_subscription()
    ################
    
    def test_get_subscription(self):
        for c in filter(None, [self.client, self.secure_client]):
            num_subscriptions = 5
            random_urls_dict = {}
            for i in range(num_subscriptions):
                random_url = "www." + str(random.randint(0, 2**20)) + ".se"
                sub = c.create_subscription(url = random_url, drop = self.drop)
                random_urls_dict[sub.id] = random_url
            for (k, v) in random_urls_dict.items():
                self.failUnlessEqual(c.get_subscription(subscription_id = k, drop = self.drop).username, v)
    
    def test_get_subscription_by_subscription(self):
        for c in filter(None, [self.client, self.secure_client]):
            url = "http://foo.com"
            sub = c.create_subscription(url = url, drop = self.drop)
            self.failUnlessEqual(c.get_subscription(subscription = sub, drop = self.drop).username, url)
    
    def test_get_subscription_no_such_subscription(self):
        for c in filter(None, [self.client, self.secure_client]):
            self.assertRaises(ResourceNotFoundError, c.get_subscription, drop = self.drop, subscription_id = 42)
    
    def test_get_subscription_no_such_drop(self):
        for c in filter(None, [self.client, self.secure_client]):
            subscription = c.create_subscription(url = "http://foo.com", drop = self.drop)
            self.assertRaises(ResourceNotFoundError, c.get_subscription, drop_name = self.available_drop_name2, subscription_id = subscription.id)
    
    def test_get_subscription_unauthorized(self):
        assert self.client2 or self.secure_client2, "no second key specified"
        for c in filter(None, [self.client, self.secure_client]):
            for c2 in filter(None, [self.client2, self.secure_client2]):
                subscription = c.create_subscription(url = "http://foo.com", drop = self.drop)
                self.assertRaises(ForbiddenError, c2.get_subscription, drop = self.drop, subscription_id = subscription.id)
    
    ################
    # delete_subscription()
    ################
    
    def test_delete_subscription(self):
        for c in filter(None, [self.client, self.secure_client]):
            num_subscriptions = 5
            random_urls_dict = {}
            for i in range(num_subscriptions):
                random_url = "www." + str(random.randint(0, 2**20)) + ".se"
                sub = c.create_subscription(url = random_url, drop = self.drop)
                random_urls_dict[sub.id] = random_url
            delete_subscription_id = random_urls_dict.keys()[0]
            delete_subscription_url = random_urls_dict[delete_subscription_id]
            c.delete_subscription(subscription_id = delete_subscription_id, drop = self.drop)
            self.failIf(delete_subscription_id in [x.id for x in c.get_subscriptions(drop = self.drop)])
            self.failIf(delete_subscription_url in [x.username for x in c.get_subscriptions(drop = self.drop)])
            self.failUnlessEqual(num_subscriptions - 1, len(list(c.get_subscriptions(drop = self.drop))))
            # clean up
            for subscription in c.get_subscriptions(drop = self.drop):
                c.delete_subscription(subscription_id = subscription.id, drop = self.drop)
    
    def test_delete_subscription_unauthorized(self):
        assert self.client2 or self.secure_client2, "no second key specified"
        for c in filter(None, [self.client, self.secure_client]):
            for c2 in filter(None, [self.client2, self.secure_client2]):
                subscription = c.create_subscription(url = "http://foo.com", drop = self.drop)
                self.assertRaises(ForbiddenError, c2.delete_subscription, drop = self.drop, subscription_id = subscription.id)

    ################
    # get_subscriptions()
    ################
    
    def test_get_subscriptions(self):
        for c in filter(None, [self.client, self.secure_client]):
            num_subscriptions = 31
            random_urls = []
            for i in range(num_subscriptions):
                random_url = "www." + str(random.randint(0, 2**20)) + ".se"
                c.create_subscription(url = random_url, drop = self.drop)
            self.failUnlessEqual(num_subscriptions, len(list(c.get_subscriptions(drop = self.drop))))
            for random_url in random_urls:
                self.failUnless(random_url in [x.username for x in c.get_subscriptions(drop = self.drop)])
            # clean up
            for subscription in list(c.get_subscriptions(drop = self.drop)):
                c.delete_subscription(subscription_id = subscription.id, drop = self.drop)
    
    def test_get_subscriptions_no_such_drop(self):
        for c in filter(None, [self.client, self.secure_client]):
            gen = c.get_subscriptions(drop_name = self.available_drop_name2)
            self.assertRaises(ResourceNotFoundError, gen.next)
    
    def test_get_subscriptions_unauthorized(self):
        assert self.client2 or self.secure_client2, "no second key specified"
        for c in filter(None, [self.client, self.secure_client]):
            for c2 in filter(None, [self.client2, self.secure_client2]):
                gen = c2.get_subscriptions(drop = self.drop)
                self.assertRaises(ForbiddenError, gen.next)

